/*ServerPageHttp类
Created on 2020年05月10日
@author: zxyong 13738196011
 */

package com.zxy.tcp;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;

import org.json.JSONObject;

import com.zxy.business.Query_Data;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.common.EncryptUtil;
import com.zxy.model.Return_Value;

public class ServerPageHttp
{
	private Return_Value		rv			= new Return_Value();
	public static final String	WEB_ROOT	= Com_Para.ApplicationPath + File.separator + "web";
	public String				strIP		= "0.0.0.0";
	public StringBuilder		strUrl		= new StringBuilder();
	public String[]				post_str	= new String[0];
	public String				strResult	= "-1";
	public String				jsonpzxyong	= "";
	private String				is_xml		= "";

	public StringBuilder PageResult()
	{
		String s_guid = "";
		StringBuilder strResult = new StringBuilder("-1");
		StringBuilder returnMessage = new StringBuilder();
		Query_Data gd = new Query_Data(this.rv);
		try
		{
			if((strUrl.indexOf("sub_code=") != -1) && (strUrl.indexOf("param_name=") != -1))
			{
				strResult.delete(0,strResult.length());
				strResult = Center_Data_Rel(strUrl,s_guid,post_str,gd,returnMessage,strIP);
			}
			// 微信支付成功
			// else if(strUrl.indexOf("WxPay.html") != -1)
			// {
			// String strTPost = "";
			// for(String strTem: post_str)
			// {
			// strTPost += "\r\n" + strTem;
			// }
			// strResult.delete(0,strResult.length());
			// strResult =
			// WxPay_Rel(strTPost,strUrl,s_guid,post_str,gd,returnMessage,strIP);
			// }
			// 支付宝登录授权
			// else if(strUrl.indexOf("aliAuth=tue") != -1)
			// {
			// strResult.delete(0,strResult.length());
			// strResult = aliAuth_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			// }
			// 支付宝获取令牌
			// else if(strUrl.indexOf("aliToken=true") != -1)
			// {
			// strResult.delete(0,strResult.length());
			// strResult =
			// aliToken_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			// }
			// 微信获取令牌
			// else if(strUrl.indexOf("wxToken=true") != -1)
			// {
			// strResult.delete(0,strResult.length());
			// strResult = wxToken_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			// }
			// 微信获取令牌
			// else if(strUrl.indexOf("wxToken2=true") != -1)
			// {
			// strResult.delete(0,strResult.length());
			// strResult = wxToken_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			// }
			// 支付宝支付成功1
			// else if(strUrl.indexOf("aliPay=tue") != -1)
			// {
			// strResult.delete(0,strResult.length());
			// strResult = aliPay_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			// }
			// 支付宝支付成功2
			// else if(strUrl.indexOf("AliPay.html") != -1)
			// {
			// strResult.delete(0,strResult.length());
			// strResult = aliPay2_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			// }
			// 支付宝支付成功3
			// else if(strUrl.indexOf("aliPay3=true") != -1)
			// {
			// strResult.delete(0,strResult.length());
			// strResult = aliPay3_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			// }
			// 其他信息
			else if((!strUrl.toString().trim().equals("")) && (strUrl.toString().trim().indexOf("GET /favicon.ico HTTP/1.1") != -1))
			{
				strResult.delete(0,strResult.length());
				strResult = Send_Error(strUrl,s_guid,post_str,gd,returnMessage);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		System.gc();
		return strResult;
	}

	// 发送错误信息
	private StringBuilder Send_Error(StringBuilder strUrl,String s_guid,String[] post_str,Query_Data gd,StringBuilder returnMessage) throws Exception
	{
		returnMessage.delete(0,returnMessage.length());
		returnMessage.append("Error请求语法错误");
		return returnMessage;
	}

	// 通用接口信息
	private StringBuilder Center_Data_Rel(StringBuilder strUrl,String s_guid,String[] post_str,Query_Data gd,StringBuilder returnMessage,String strIP) throws Exception
	{
		StringBuilder strAry = new StringBuilder();
		Hashtable<String,String> htParam = new Hashtable<String,String>();
		for(String strTemV: strUrl.toString().split("&"))
		{
			String[] strTemPar = strTemV.split("=");
			if(strTemPar.length == 2)
				htParam.put(strTemPar[0],strTemPar[1]);
			else htParam.put(strTemPar[0],"");
		}
		String sub_code = htParam.get("sub_code") == null ? "" : (String)htParam.get("sub_code");
		String sub_usercode = htParam.get("sub_usercode") == null ? "" : (String)htParam.get("sub_usercode");
		String delay_data = htParam.get("delay_data") == null ? "" : (String)htParam.get("delay_data");
		String delay_code = htParam.get("delay_code") == null ? "" : (String)htParam.get("delay_code");
		String param_name = htParam.get("param_name") == null ? "" : (String)htParam.get("param_name");
		String session_id = htParam.get("session_id") == null ? "" : (String)htParam.get("session_id");
		is_xml = htParam.get("is_xml") == null ? "" : (String)htParam.get("is_xml");
		String jsonpzxyong = htParam.get("jsoncallback") == null ? "" : (String)htParam.get("jsoncallback");
		s_guid = htParam.get("s_guid") == null ? "" : (String)htParam.get("s_guid");
		this.rv.param_name = param_name;
		// 是否需要登录flag
		boolean bFlag = false;
		if((!Com_Para.Is_UserLogin.equals("")) && (Com_Para.Is_UserLogin.indexOf("[" + param_name + "]") != -1))
		{
			bFlag = true;
		}
		// 传递in 参数
		ArrayList<String> aryParamValue = new ArrayList<String>();
		for(int i = 1;i < 101;i++)
		{
			boolean tembFlag = false;
			String strVS = "";
			if(htParam.get("param_value" + String.valueOf(i)) != null)
			{
				strVS = (String)htParam.get("param_value" + String.valueOf(i));
				if(i == 1 || i == 2)
					strVS = Com_Fun.getParam_value1(strVS.trim());
				tembFlag = true;
			}
			else
			{
				for(String temstr: post_str)
				{
					if(temstr.indexOf("param_value" + String.valueOf(i) + "=") == 0)
					{
						tembFlag = true;
					}
					else if(temstr.equals("jsonpzxyong"))
						jsonpzxyong = temstr.split("=")[1];
					if((temstr.indexOf("param_value" + String.valueOf(i) + "=") == 0) && (temstr.split("=").length > 1))
					{
						strVS = temstr.split("=")[1];
						if(i == 1 || i == 2)
							strVS = Com_Fun.getParam_value1(strVS);
						break;
					}
				}
			}

			if(!tembFlag)
				break;
			if((bFlag) && (i == 2))
			{
				if(Com_Para.EncryptUtilType == 1)
					strVS = EncryptUtil.getSM32(strVS);
				else strVS = EncryptUtil.getMd5(strVS).toLowerCase();
			}
			aryParamValue.add(i - 1,strVS);
		}
		htParam.clear();
		htParam = null;

		// 用户登录接口
		if(bFlag)
		{
			gd.session_id = Com_Fun.Get_New_GUID();
			strAry = gd.GetDataList(sub_code,sub_usercode,param_name,aryParamValue,strUrl,delay_data,delay_code,strIP);
		}
		// 非登录接口但需要session_id验证
		else if((Com_Para.Login_Session == 1) && (Com_Fun.GetInterSession(param_name)))
		{
			gd.session_id = session_id;
			if(Com_Fun.GetInterSession(param_name))
			{
				if((session_id.equals("")) || (!Com_Fun.HadSession(session_id,session_id)))
				{
					strAry.delete(0,strAry.length());
					strAry.append("{\"" + param_name + "\":[{\"session_id\":\"\",\"s_result\":\"0\",\"error_desc\":\"用户未登陆,请登陆\"}]}");
				}
				else
				{
					strAry = gd.GetDataList(sub_code,sub_usercode,param_name,aryParamValue,strUrl,delay_data,delay_code,strIP);
				}
			}
			else
			{
				strAry = gd.GetDataList(sub_code,sub_usercode,param_name,aryParamValue,strUrl,delay_data,delay_code,strIP);
			}
		}
		// 通用接口
		else
		{
			strAry = gd.GetDataList(sub_code,sub_usercode,param_name,aryParamValue,strUrl,delay_data,delay_code,strIP);
		}

		if(!jsonpzxyong.equals(""))
		{
			strAry.insert(0,jsonpzxyong + "(").append(")");
		}
		if(this.rv.s_result == 1)
			this.rv = gd.rv;
		if(is_xml.equals("1"))
		{
			String temMsg = strAry.toString();
			JSONObject jso = null;
			jso = new JSONObject(temMsg);
			temMsg = Com_Fun.JSONToXml(jso.get(param_name).toString(),param_name);
			strAry.delete(0,strAry.length());
			strAry.append(temMsg);
		}
		// 清理垃圾内存
		// System.gc();
		return strAry;
	}
}